package n_swing_jtable;

import java.awt.BorderLayout;

import javax.swing.ButtonGroup;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SortOrder;
import javax.swing.RowSorter.SortKey;
import javax.swing.SwingUtilities;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableStringConverter;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;

public class JTableEx3 extends JInternalFrame{

	private static final long serialVersionUID = 1L;
	ButtonGroup bgrp = new ButtonGroup();
	TableRowSorter sorter ;
	boolean sortFlag = false;
	
	private JPanel jContentPane = null;
	private JScrollPane jScrollPane = null;
	private JTable jTable = null;
	private JPanel panel;
	private JLabel status;
	private JPanel panel_1;
	private JCheckBox chckbxNewCheckBox;

	/**
	 * This is the default constructor
	 */
	public JTableEx3() {
		super("AbstractTableModel 1",true, true, true, false);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(565, 252);
		this.setContentPane(getJContentPane());
		this.setTitle("Table Test");
		this.setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getPanel(), BorderLayout.SOUTH);
			jContentPane.add(getPanel_1(), BorderLayout.NORTH);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTable() {
		if (jTable == null) {
			TableTestModel3 ttm = new TableTestModel3();
			jTable = new JTable(ttm);
			
			jTable.setRowSorter(new TableRowSorter<TableModel>(ttm));
			TableColumnModel tModel = jTable.getColumnModel();
		
//			jTable.getRowSorter().toggleSortOrder(0);// 오름차순정렬(문자)
			
			// 내림차순 정렬(문자)
			sorter = (TableRowSorter)jTable.getRowSorter();
//			SortKey sortKey = new SortKey(2, SortOrder.DESCENDING);
//			List<SortKey> keys = new ArrayList<>();
//			keys.add(sortKey);
//			sorter.setSortKeys(keys);
//			sorter.sort();
			
			// 숫자형 소스
			jTable.setRowSorter(sorter);
//			sorter.setComparator(2, new NumberSortCompare());
			
			tModel.getColumn(0).setPreferredWidth(60);
			tModel.getColumn(1).setPreferredWidth(100);
			tModel.getColumn(2).setPreferredWidth(200);
			tModel.getColumn(3).setPreferredWidth(80);

			
			
			jTable.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseReleased(java.awt.event.MouseEvent e) {
					
					int c = jTable.getSelectedColumn();
					int r = jTable.getSelectedRow();
					if(c == 2){
						status.setText(""+(Integer)jTable.getValueAt(r, c));
					}else{
						status.setText((String)jTable.getValueAt(r, c));
					}
					
				}
			});
			
			
		}
		return jTable;
	}
	
	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JTableEx3 thisClass = new JTableEx3();
				thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				thisClass.setVisible(true);
			}
		});
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getStatus());
		}
		return panel;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("");
			status.setBackground(SystemColor.inactiveCaption);
			status.setOpaque(true);
			status.setPreferredSize(new Dimension(0, 24));
		}
		return status;
	}

	
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.add(getChckbxNewCheckBox());
		}
		return panel_1;
	}
	
	private JCheckBox getChckbxNewCheckBox() {
		if (chckbxNewCheckBox == null) {
			chckbxNewCheckBox = new JCheckBox("숫자형으로 정렬");
			chckbxNewCheckBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sortFlag = chckbxNewCheckBox.isSelected();
					if(sortFlag){
						sorter.setComparator(2, new NumberSortCompare());
					}else{
						sorter.setComparator(2, null);
					}
				}
			
			});
		}
		return chckbxNewCheckBox;
	}
	
	// inner number sort class
	class NumberSortCompare implements Comparator{
		
		public NumberSortCompare(){}
		
		@Override
		public int compare(Object o1, Object o2) {
			int r=0;
			int n1 = Integer.valueOf((String)o1);
			int n2 = Integer.valueOf((String)o2);
			
			r = (sortFlag)? n1-n2 : n2-n1;
			
			return r;
		}
		
	}
	
	

}  
